package dyyx;

import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContextEvent;

import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

import dyyx.util.CommUtil;
import dyyx.util.ConfigUtil;
import dyyx.util.KafkaUtil;
import dyyx.util.RunStatusUtil;
import dyyx.util.SpringUtil;
import dyyx.util.WebUtil;

// public class AppListener implements ServletContextListener{
public class AppListener extends ContextLoaderListener{
	
	public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
	
	private static final String APPTYPE_PROVIDER = "provider";
	private static final String APPTYPE_CONSUMER = "consumer";


	private static final Set<String> APPTYPES = new HashSet<>();
	
	static{
		APPTYPES.add(APPTYPE_PROVIDER);
		APPTYPES.add(APPTYPE_CONSUMER);

	}
	

	
	public void contextInitialized(ServletContextEvent event) {
		System.out.println(this.getClass().getSimpleName()+".init run,"+new Date());
		
		Map<String,Object> kafkaConfigMap = KafkaUtil.getConfigMap();
		System.out.println("kafkaConfigMap="+kafkaConfigMap);

		
		String webRootDir = event.getServletContext().getRealPath("/");
		System.out.println("webRootDir="+webRootDir);
		
		// org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext, ServletContext)
		// String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
		
		String apptype = WebUtil.getAppType();
		if(CommUtil.isBlank(apptype)){
			apptype = APPTYPE_PROVIDER;
		}
		
		if(!APPTYPES.contains(apptype)){
			throw new RuntimeException("apptype error,"+apptype);
		}
		
		RunStatusUtil.updateStaticInfo("apptype", apptype);

		
		String contextConfigLocation = "classpath:spring/dubbo-"+apptype+".xml";
		
		event.getServletContext().setInitParameter(CONFIG_LOCATION_PARAM, contextConfigLocation);
		
		String contextConfigLocation2 = event.getServletContext().getInitParameter(CONFIG_LOCATION_PARAM);
		
		String info = contextConfigLocation +","+contextConfigLocation2;
		RunStatusUtil.updateStaticInfo("contextConfigLocation", info);
		
		
		WebUtil.setWebRootDir(webRootDir);
		
		ConfigUtil.getAllConfig();
		
	
		System.out.println("startTime="+RunStatusUtil.runStatus.startTime);
		
		long start = System.currentTimeMillis();
		super.contextInitialized(event);
		long end = System.currentTimeMillis();
		long time  = end - start;
		RunStatusUtil.updateStaticInfo("contextInitializedTime", time+"");

		WebApplicationContext webApplicationContext = ContextLoaderListener.getCurrentWebApplicationContext();
		SpringUtil.setApplicationContext(webApplicationContext);
		System.out.println("webApplicationContext="+webApplicationContext);
		System.out.println("contextInitializedTime="+time);


		// AllInOneService allInOneService = (AllInOneService)webApplicationContext.getBean("allInOneServiceClient");
		// System.out.println("allInOneService="+allInOneService);
		// System.out.println(allInOneService.echo("hello,"+LocalDateTime.now()));

    	
	}
	
	public void contextDestroyed(ServletContextEvent event) {
		super.contextDestroyed(event);
	
		System.out.println(this.getClass().getSimpleName()+".contextDestroyed run "+System.currentTimeMillis());
    	
	}
}